package com.juzipi.demo.controller;

import com.juzipi.demo.pojo.PageResult;
import com.juzipi.demo.pojo.Student;
import com.juzipi.demo.service.PracticeTkmapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("tk")
public class PracticeTkmapperController {


    @Autowired
    private PracticeTkmapperService practiceTkmapperService;


    /**
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<List<Student>> queryStuExample(@PathVariable Integer id){

        List<Student> students = practiceTkmapperService.queryStuExample(id);
        if (StringUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);

    }


    /**
     * 测试 selectAll 方法
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Student>> queryAllStu(){
        List<Student> students = practiceTkmapperService.queryAllStu();
        if (CollectionUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);
    }


    /**
     * 测试 selectByPrimaryKey 方法
     * @param id
     * @return
     */
    @GetMapping("key/{id}")
    public ResponseEntity<Student> queryStu(@PathVariable Integer id){

        Student student = practiceTkmapperService.queryStu(id);
        if (StringUtils.isEmpty(student)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(student);

    }



    @GetMapping("listall/{deptId}")
    public ResponseEntity<List<Student>> queryStuListAll(@PathVariable Integer deptId){
        List<Student> students = practiceTkmapperService.queryStuListAll(deptId);
        if (CollectionUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);
    }



    @PostMapping("name")
    public ResponseEntity<List<Student>> queryStuBlurry(@RequestParam(value = "key",required = false)String key){
        List<Student> students = practiceTkmapperService.queryStuBlurry(key);
        if (CollectionUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);
    }



    @GetMapping("one/{id}")
    public ResponseEntity<Student> queryStuCount(@PathVariable Integer id){
        Student student = practiceTkmapperService.queryStuCount(id);
        if (StringUtils.isEmpty(student)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(student);

    }


    @PostMapping("part")
    public ResponseEntity<PageResult<Student>> queryStuByPart(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "10")Integer rows
    ){
        PageResult<Student> students = practiceTkmapperService.queryStuByPart(key,page,rows);
        if (StringUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);

    }


    /**
     * 查询年龄最高的学生
     * @param key
     * @return
     */
    @PostMapping("age")
    public ResponseEntity<List<Student>> queryStudentMaxAge(@RequestParam(value = "key",required = false)String key){

        List<Student> students = practiceTkmapperService.queryStudentMaxAge(key);
        if (CollectionUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);
    }


    /**
     * 查询某一系部的所有学生
     * @param key
     * @return
     */
    @PostMapping("dept")
    public ResponseEntity<List<Student>> queryStudentDept(@RequestParam(value = "key",required = false)String key){
        List<Student> students = practiceTkmapperService.queryStudentDept(key);
        if (CollectionUtils.isEmpty(students)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(students);




    }



}
